﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.IO;

namespace Bu
{
    public partial class Question7
    {

        AddressBookInfo AddressBook;

        public Question7()
        {
            this.InitializeComponent();

            LoadAddressBook();

            foreach (IndexCard c in this.pnlTabs.Children.OfType<IndexCard>().ToList<IndexCard>())
            {
                c.MouseLeftButtonDown += new MouseButtonEventHandler(SelectCard);
                c.Margin = new Thickness(0, 0, 0, 0);
                c.Width = 150d;
                c.Height = 40d;
                c.BorderThickness = new Thickness(2, 2, .5, 0);
            }
        }

        void SelectCard(object sender, MouseButtonEventArgs e)
        {
            foreach (IndexCard c in this.pnlTabs.Children.OfType<IndexCard>().ToList<IndexCard>())
            {
                c.Margin = new Thickness(-10, 1, 0, 0);
                c.Width = 150d;
                WrapPanel p = (WrapPanel)c.LayoutRoot.Children[0];
                Border b = p.Children[0] as Border;
                b.Width = 140d;
                c.Height = 40d;
                c.BorderThickness = new Thickness(2, 2, .5, 0);
            }
            IndexCard card = sender as IndexCard;
            card.Select();
            
            //Load the picture. The 'using' construct ensures the resource will be released.
            try
            {
                Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\Assets\\" + card.PersonData.Picture);
                BitmapImage bitmapImage = new BitmapImage(uri);
                this.imgPhoto.Source = bitmapImage;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hmm, the image appears to be bogus.");
            }
            //Fill the rest of the personal data
            this.txtName.Text = card.PersonData.Name;
            this.txtAddress.Text = card.PersonData.Address;
            this.txtCity.Text = card.PersonData.City;
            this.txtState.Text = card.PersonData.State;
            this.txtZip.Text = card.PersonData.Zip;
            this.txtPhone.Text = card.PersonData.Phone;
            this.txtEmail.Text = card.PersonData.Email;
        }

        /// <summary>
        /// This function is a pretty descent example of Xml serialization in .net. 
        /// First an xml document is loaded from disk. Next, in order to deserialize
        /// the xml, a specialized stream reader (XmlNodeReader) is initialized with 
        /// the document element of the xml document object. An XmlSerializer is
        /// created with the type information required to create an AddressBookInfo
        /// object. Finally, the stream reader is passed into the Deserialize() method
        /// of the XmlSerializer to create an AddressBook object. 
        /// 
        /// Here's where the interface pixie dust comes into play. Because the AddressBook
        /// class implements ISerializable, the interface method ReadXml must be implemented.
        /// This method gets called automagically when an ISerializable object gets deserialized.
        /// The xml file in question contains a collection of Person nodes which will be
        /// read to populate the List<Person> member variable of the AddressBook class.
        /// Because the Person class is a simple class, I chose to use declaritavely mark each
        /// propery access method with an XmlElement attribute. This provides the runtime with
        /// the necessary information to populate the class members of the Person object.
        /// </summary>
        private void LoadAddressBook()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("AddressBook.xml"); 

            XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
            XmlSerializer ser = new XmlSerializer(typeof(AddressBookInfo));
            object obj = ser.Deserialize(reader);
            AddressBook = (AddressBookInfo)obj;

            foreach (Person p in AddressBook.People)
            {
                IndexCard card = new IndexCard();
                TextBlock t = card.LayoutRoot.FindName("txtName") as TextBlock;
                if (t != null)
                {
                    t.Text = p.Name;
                }
                card.PersonData = p; // we'll need this later to retrieve the rest of the data
                pnlTabs.Children.Add(card);
            }
        }

        private void LoadName(string name)
        {
        
        }
    }

    /// <summary>
    /// Each property access method gets an XmlElement attribute
    /// to provide the necessary info to deserialize the class.
    /// </summary>
    public class Person
    {
        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Address")]
        public string Address { get; set; }

        [XmlElement("City")]
        public string City { get; set; }

        [XmlElement("State")]
        public string State { get; set; }

        [XmlElement("Zip")]
        public string Zip { get; set; }

        [XmlElement("Email")]
        public string Email { get; set; }

        [XmlElement("Phone")]
        public string Phone { get; set; }

        [XmlElement("Picture")]
        public string Picture { get; set; }


    }
    public class AddressBookInfo:IXmlSerializable
    {
        [XmlArrayItem("Person")]
        public List<Person> People;
     
        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return new System.Xml.Schema.XmlSchema();
        }

        public void ReadXml(XmlReader reader)
        {
            // read through the Person nodes and add them to the list
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(reader.ReadOuterXml());
            this.People = new List<Person>();
            foreach (XmlElement xp in doc.DocumentElement.ChildNodes)
            {
                XmlNodeReader personReader = new XmlNodeReader(xp);
                XmlSerializer ser = new XmlSerializer(typeof(Person));
                object obj = ser.Deserialize(personReader);
                this.People.Add((Person)obj);
            }
        }

        public void WriteXml(XmlWriter writer)
        {
        }

        #endregion
    }
        
}

